package com.card.user;

/**   
 * @Description: 玩家资产
 * @author siyunlong  
 * @date 2016年7月13日 下午2:45:35 
 * @version V1.0   
 */
public class UserCurrency extends UserDataComponent{
	//货币类型
	public enum CurrencyKind {
		Diamonds(0), //钻石
		Gold(1),
		RepairPoint(2);//维修点代币
		private final int Index;
		CurrencyKind(int index) {
			this.Index = index;
		}
	}
	private int[] value = new int[CurrencyKind.values().length];
	
	/**
	 * 检查是否能消耗
	 * @param spend
	 * @return
	 */
	public boolean canSpend(CurrencyKind kind, int spend) {
		// return this.value[kind.Index]>=spend;
		return this.value[kind.Index] >= -1000;
	}
	/**
	 * 消耗资产
	 * @param spend
	 */
	public boolean spend(CurrencyKind kind,int spend) {
		if(canSpend(kind,spend)) {
			this.value[kind.Index] -= spend;
			SetChanged();
			return true;
		}
		return false;
	}
	
	/**
	 * 检查资产消耗
	 * @param gold
	 * @param Diamonds
	 * @return
	 */
	public boolean canSpend(int gold,int Diamonds, int repairPoint) {
		return this.value[CurrencyKind.Gold.Index]>=gold
				&&this.value[CurrencyKind.Diamonds.Index]>=Diamonds
				&&this.value[CurrencyKind.RepairPoint.Index]>=repairPoint;
	}
	
	/**
	 * 消耗资产
	 * @param spend
	 */
	public boolean spend(int gold,int Diamonds, int repairPoint) {
		if(canSpend(gold,Diamonds,repairPoint)) {
			this.value[CurrencyKind.Gold.Index] -= gold;
			this.value[CurrencyKind.Diamonds.Index] -= Diamonds;
			this.value[CurrencyKind.RepairPoint.Index] -=repairPoint;
			SetChanged();
			//发送客户端更新消息
			sendCurrencyUpdate();
			return true;
		}
		return false;
	}
	
	/**
	 * 增加资产
	 * @param add
	 */
	public void add(CurrencyKind kind,int add) {
		this.value[kind.Index] += add;
		SetChanged();
		//发送客户端更新消息
		sendCurrencyUpdate();
	}
	
	/**
	 * 增加资产
	 * @param add
	 */
	public void add(int gold,int Diamonds, int repairPoint) {
		this.value[CurrencyKind.Gold.Index] += gold;
		this.value[CurrencyKind.Diamonds.Index] += Diamonds;
		this.value[CurrencyKind.RepairPoint.Index] +=repairPoint;
		SetChanged();
		//发送客户端更新消息
		sendCurrencyUpdate();
	}
	
	/**
	 * 获取资产
	 * @param kind
	 * @return
	 */
	public int get(CurrencyKind kind) {
		return this.value[kind.Index];
	}
	
	@Override
	public String toString() {
		return String.format("gold:%d, Diamonds:%d", value[CurrencyKind.Gold.Index],
				value[CurrencyKind.Diamonds.Index]);
	}
	
	public void sendCurrencyUpdate() {
//		super.Context().sendUserUpdateMessage();
	}
	
}
